//
//  UIColor+RGBColor.m
//  Pods
//
//  Created by William Chang on 16/12/28.
//
//

#import "UIColor+RGBColor.h"

@implementation UIColor (RGBColor)

/**
 *  转换#FFFFFF格式颜色
 *
 *  @param string 颜色字符串
 *
 *  @return
 */
+ (UIColor *)RGBColor:(NSString *)string {
    if ([string rangeOfString:@"#"].location != NSNotFound) {
        string = [string stringByReplacingOccurrencesOfString:@"#" withString:@""];
    }
    
    // 十六进制字符串转成整形。
    long colorLong = strtoul([string cStringUsingEncoding:NSUTF8StringEncoding], 0, 16);
    // 通过位与方法获取三色值
    int R = (colorLong & 0xFF0000 )>>16;
    int G = (colorLong & 0x00FF00 )>>8;
    int B = colorLong & 0x0000FF;
    
    //string转color
    UIColor *wordColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0];
    return wordColor;
}


/**
 *  转换#FFFFFF格式颜色
 *
 *  @param string 颜色字符串
 *  @param alpha  透明度
 *
 *  @return 颜色
 */
+ (UIColor *)RGBColor:(NSString *)string alpha:(CGFloat )alpha {
    if ([string rangeOfString:@"#"].location != NSNotFound) {
        string = [string stringByReplacingOccurrencesOfString:@"#" withString:@""];
    }
    
    // 十六进制字符串转成整形。
    long colorLong = strtoul([string cStringUsingEncoding:NSUTF8StringEncoding], 0, 16);
    // 通过位与方法获取三色值
    int R = (colorLong & 0xFF0000 )>>16;
    int G = (colorLong & 0x00FF00 )>>8;
    int B = colorLong & 0x0000FF;
    
    //string转color
    UIColor *wordColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:alpha];
    return wordColor;
}

@end
